use strict;
use warnings;
use XML::Simple;

my $var;
my @service;
my @param;

logo();
main();

sub main {
  my $p;
  print "Welcome" . "\n" . "Bonjour" . "\n";
  print "This modules will help you to create a service" . "\n" . "Ce module va vous aidez a cree un service" . "\n";
  print "\n";
  groupe();
  name();
  type();
  response();
  create();
  $p = XMLread();
  XMLwrite( $p );
  if ( -e "tmp.xml" ) {
    unlink "tmp.xml";
  }
}

sub groupe {
  print "Name of groupe" . "\\" . "Nom du groupe" . "> ";
  $var = <stdin>;
  chop $var;
  if ( $var eq "" ) {
    groupe();
  }
  else {
    $service[0] = $var;
  }
}

sub name {
  print "Name of service" . "\\" . "Nom du service" . "> ";
  $var = <stdin>;
  chop $var;
  if ( $var eq "" ) {
   name();
  }
  else {
    $service[1] = $var;
  }
}

sub type {
  print "Type of service" . "\\" . "Type du service" . " " . "[simple/complex]" ." > ";
  $var = <stdin>;
  chop $var;
  if ( $var =~ m/^simple$/i ) {
    $service[2] = $var;
    $service[3] = 0;
    return;
  }
  if ( $var =~ m/^complex$/i ) {
    $service[2] = $var;
    num();
  }
  else {
    type();
  }
}

sub num {
  print "Number of parameters" . "\\" . "Nombre de parametre" . "> ";
  $var = <stdin>;
  chop $var;
  if ( $var =~ m/^[0-9]$/i ) {
    $service[3] = $var;
    numParam();
  }
  else {
    num();
  }
  
}
  
sub numParam {
  my ( $name, $i );
  for ( $i = 1; $i <= $var; $i++ ) {
    print "Name of parameters $i" . "\\" . "Nom du parametre $i" . "> ";
    $name = <stdin>;
    chop $name;
    if ( $name eq "" ) {
      $name = <stdin>;
      chop $name;
    }
    $param[$i] = $name;
  }
}

sub response {
  print "Name of service response" . "\\" . "Nom du service de reponse" . "> ";
  $var = <stdin>;
  chop $var;
  $service[4] = $var;
  if ( $var eq "" ) {
    response();
  }
}

sub XMLread {
  my ( $g, $p );
  $p = 0;
  $g = "<" . $service[0] . ">";
  open List, "< SERVICE\\service.xml";
  while ( <List> ) {
    chomp;
    if ( $_ =~ m/^$g$/i ) {
      $p = 1;
    }
  }
  close List;
  return $p;
}

sub XMLwrite {
  my ( $p, $g );
  my @list;
  $p = $_[0];
  $g = "<" . $service[0] . ">";
  open List, "< tmp.xml";
  while ( <List> ) {
    chomp;
    if ( $_ ne "" ) {
      push @list, $_;
    }
  }
  close List; 
  print "================================" . "\n";
  print "GROUPE : " . $service[0] . "\n";
  if ( $p == 1 ) {
    print "/!\\ " . "GROUP EXIST" . "\\" . "GROUPE EXISTANT" . " /!\\" . "\n";
    print "Copy / Paste in SERVICE\\service.xml" . " | " . "Copie / colle dans SERVICE\\service.xml" . "\n";
    print "================================" . "\n";
    foreach ( @list ) {
      print $_ . "\n";
    }
  }
  else {
    open WRITER, ">> SERVICE\\service.xml";
    foreach ( @list ) {
      print WRITER $_ . "\n";
    }
    print "Write is finish" . "\\" . "Ecriture fini" . "\n";
    close WRITER;
  }
  print "================================" . "\n";
  print "================================" . "\n";
  print "DONE" . "\\" . "Fini" . "\n";
  print "================================" . "\n";
  print "ADN-TEAM" . "\n";
}

sub create {
  my ( $g, $n, $data, $function, $smpl, $i );
  $smpl = XML::Simple->new( NoSort => '1',
			    KeepRoot => '1',
			    NoAttr => '1' );
  $g = $service[0];
  $n = $service[1];
  $data->{$g}->{$n}->{type} = $service[2]; 
  $data->{$g}->{$n}->{param} = $service[3];
  for ( $i = 1; $i < @param; $i++ ) {
    $data->{$g}->{$n}->{"p$i"} = $param[$i];
  }
  $data->{$g}->{$n}->{response} = $service[4];
  $function = $g . "_" . $n . ".pl";
  $data->{$g}->{$n}->{function} = $function;
  $i = valide();
  if ( $i == 0 ) {
    $smpl->XMLout( $data,
		   NoAttr => '1',
		   NoSort => '0',
		   RootName => $g,
		   OutputFile => 'tmp.xml' );
  }
}

sub valide {
  my $i;
  print "================================" . "\n";
  print "GROUPE : " . $service[0] . "\n";
  print "SERVICE : " . $service[1] . "\n";
  print "TYPE : " . $service[2] . "\n";
  print "PARAM : " . $service[3] . "\n";
  for ( $i = 1; $i < @param; $i++ ) {
    print "\t" . "- " . $param[$i] . "\n";
  }
  print "RESPONSE : " . $service[4] . "\n";
  print "FUNCTION : " . $service[0] . "_" . $service[1] . ".pl" . "\n";
  print "================================" . "\n";
  print "Values good" . "\\" . "Donee correcte" . " [yes/no]" . " > ";
  $var = <stdin>;
  if ( $var =~ m/^yes$/i ) {
    return 0;
  }
  if ( $var =~ m/^no$/i ) {
    main();
  }
  else {
    valide();
  }
}

sub logo {
  print <<MESS;
\t\t     **      *******    ****     **
\t\t    ****    /**////**  /**/**   /**
\t\t   **//**   /**    /** /**//**  /**
\t\t  **  //**  /**    /** /** //** /**
\t\t ********** /**    /** /**  //**/**
\t\t/**//////** /********  /**   //****
\t\t//      //  ////////   ///    ////
\t\t\t    SERVICE     \tCopyright 2009 - ADN
\t\t
MESS
}
